<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class My extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     * 
     */
    public function index()
    {
        $people = model('People')->people($this->request->post('openid'));
        return json_encode($people);
    }
    // 我的卡卷
    public function coupon(){
        $people = model('People')->people($this->request->post('openid'));
        $res = Db::name('order')->where('people_id',$people['id'])->select();
        foreach ($res as $k=>$v){
           // 判断是否过期
            if($v['status'] == 2){
                $endtime = Db::name('spike')->where('id',$v['goods_id'])->value('etime');
                if (time() > $endtime){
                    Db::name('order')->where('id',$v['id'])->update(['type'=>3]);
                }
            }else{
                $endtime = Db::name('integral')->where('id',$v['goods_id'])->value('endtime');
                if (time() > strtotime($endtime)){
                    Db::name('order')->where('id',$v['id'])->update(['type'=>3]);
                }
            }
        }
        $cartsW = Db::name('order')->where(array('people_id'=>$people['id'],'type'=>1))->select();
        foreach ($cartsW as $k=>$v){
            if($v['status'] == 2){
                $goods = Db::name('spike')->where('id',$v['goods_id'])->find();
                $cartsW[$k]['image'] = $goods['image'];
                $cartsW[$k]['name'] = $goods['name'];
                $cartsW[$k]['time'] = $goods['stime'] . ' - ' .$goods['etime'];
            }else{
                $goods = Db::name('integral')->where('id',$v['goods_id'])->find();
                $cartsW[$k]['image'] = $goods['image'];
                $cartsW[$k]['name'] = $goods['name'];
                $cartsW[$k]['time'] = $goods['strattime'] . ' - ' .$goods['endtime'];
            }
        }
        $cartsY = Db::name('order')->where(array('people_id'=>$people['id'],'type'=>2))->select();
        foreach ($cartsY as $k=>$v){
            if($v['status'] == 2){
                $goods = Db::name('spike')->where('id',$v['goods_id'])->find();
                $cartsY[$k]['image'] = $goods['image'];
                $cartsY[$k]['name'] = $goods['name'];
                $cartsY[$k]['time'] = $goods['stime'] . ' - ' .$goods['etime'];
            }else{
                $goods = Db::name('integral')->where('id',$v['goods_id'])->find();
                $cartsY[$k]['image'] = $goods['image'];
                $cartsY[$k]['name'] = $goods['name'];
                $cartsY[$k]['time'] = $goods['strattime'] . ' - ' .$goods['endtime'];
            }
        }
        $cartsG = Db::name('order')->where(array('people_id'=>$people['id'],'type'=>3))->select();
        foreach ($cartsG as $k=>$v){
            if($v['status'] == 2){
                $goods = Db::name('spike')->where('id',$v['goods_id'])->find();
                $cartsG[$k]['image'] = $goods['image'];
                $cartsG[$k]['name'] = $goods['name'];
                $cartsG[$k]['time'] = $goods['stime'] . ' - ' .$goods['etime'];
            }else{
                $goods = Db::name('integral')->where('id',$v['goods_id'])->find();
                $cartsG[$k]['image'] = $goods['image'];
                $cartsG[$k]['name'] = $goods['name'];
                $cartsG[$k]['time'] = $goods['strattime'] . ' - ' .$goods['endtime'];
            }
        }
        return json_encode(['cartsW'=>$cartsW,'cartsY'=>$cartsY,'cartsG'=>$cartsG]);
    }
    // 卡卷详情
    public function detail(){
        $params = $this->request->post();
        $res = Db::name('order')->where('id',$params['id'])->find();
        if($res['status'] == 2){
            $goods = Db::name('spike')->where('id',$res['goods_id'])->find();
            $goods['code'] = $res['order_no'];
            $goods['time'] = $goods['etime'];
            $goods['ewm'] = $res['ewm'];
            $goods['type'] = $res['type'];
        }else{
            $goods = Db::name('integral')->where('id',$res['goods_id'])->find();
            $goods['code'] = $res['order_no'];
            $goods['time'] = $goods['endtime'];
            $goods['ewm'] = $res['ewm'];
            $goods['type'] = $res['type'];
        }
        $people = model('People')->people($this->request->post('openid'));
        $status = Db::name('scanning')->where('people_id',$people['id'])->count();
        return json_encode(['res'=>$goods,'status'=>$status]);
    }
    // 我的收藏
    public function keep(){
        $people = model('People')->people($this->request->post('openid'));
        $res = Db::name('keep')
            ->join('x_merchant','x_keep.merchant_id = x_merchant.id')
            ->where('x_keep.people_id',$people['id'])
            ->field('x_merchant.*')
            ->select();
        return json_encode($res);
    }
    // 我的活动
    public function activity(){
        $people = model('People')->people($this->request->post('openid'));
        $res = Db::name('apply')
            ->join('x_activity','x_apply.activity_id = x_activity.id')
            ->where('x_apply.people_id',$people['id'])
            ->field('x_activity.*')
            ->select();
        foreach ($res as $k=>$v){
            $res[$k]['starttime'] = date('Y-m-d',$v['starttime']);
        }
        return json_encode($res);
    }
    public function member(){
        $res = Db::name('config')->where('id',18)->find();
        return json_encode($res);
    }
    // 订单验证
    public function yanz(){
        $id = $this->request->post('id');
        $res = Db::name('order')->where('id',$id)->update(['type'=>2]);
        return $res?200:400;
    }
    // 我的车牌
    public function myCard(){
        $people = model('People')->people($this->request->post('openid'));
        $res = Db::name('people')->where('id',$people['id'])->value('card');
        return json_encode($res);
    }
    // 车牌绑定
    public function updateCard(){
        $params = $this->request->post();
        $people = model('People')->people($params['openid']);
        $res = Db::name('people')->where('id',$people['id'])->update(['card'=>$params['card']]);
        return $res?200:400;
    }
    // 调用停车后去优惠券接口
    public function tcApi(){
        date_default_timezone_set('PRC');
        $params = $this->request->post();
        $str = time().str_replace(array(" ","-","_"),'',$params['openid']);
        $rest = substr($str, 0, 30);
//        $bh = $this->bianhao($rest);
//        $res = json_decode($bh,true);
//        dump($res);die;
        $start = strtotime(date("Y-m-d 08:00:00",time())).'000';
        $end = strtotime(date("Y-m-d 23:59:59",time())).'000';
        $time = time().'000';
        $data = array(
            'appkey'=>'6S7YK1E5',
            'couponSource'=> '4001',
            'deductRuleType'=> '3',
            'parkCodes'=>'10119032716423002000',
            'deductContent'=> '180',
            'generateObj'=> '1',
            'limitFreeTime'=> '180',
            'nonce'=> $rest,
            'plateNo'=> $params['card'],
            'startTime'=> $start,
            'endTime'=> $end,
            'time'=> $time
        );
        $secret = '7864510C3C9E981C826A3D32CC3F33FA';
        //$data=array_filter($data);
        ksort($data);
        $str='';
        foreach($data as $k=>$v) {
            $str.=$k.'='.$v.'&';
        }
        $str.='secret='.$secret;
//        dump($str);die;
        $strs = strtoupper(md5($str));
        $url = "http://www.hikparking.com:8086/cloud/service/generateCoupon?token=".$strs;
        $res = $this->curl($url,json_encode($data));
        return json_encode($res);
    }
    // 获取编号集
    public function bianhao($rest){
        $data = array(
            'appkey'=>'6S7YK1E5',
            'nonce'=> $rest,
            'pageSize'=> 400,
            'pageNo'=> 1,
            'time'=> time().'000'
        );
        $secret = '7864510C3C9E981C826A3D32CC3F33FA';
        ksort($data);
        $str='';
        foreach($data as $k=>$v) {
            $str.=$k.'='.$v.'&';
        }
        $str.='secret='.$secret;
//        dump($str);die;
        $strs = strtoupper(md5($str));
        $url = "http://www.hikparking.com:8086/cloud/service/getParkingInfos?token=".$strs;
        $res = $this->curl($url,json_encode($data));
        return json_encode($res);
    }
    function curl($url,$data) {
        $headers = array(
            "Content-type:application/json;charset='utf-8'",
            "Content-Length:".strlen($data)
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $tmpInfo = curl_exec($ch);
        $error = curl_errno($ch);
        curl_close($ch);
        if ($error) {
            return $error;
        }else{
            return $tmpInfo;
        }
    }
}
